/* eslint-disable no-undef */
const { defineConfig } = require("@vue/cli-service");
const webpack = require("webpack");
const PORT = 8099; // 端口配置
const TITLE = 'Chart'
// process.env.VUE_APP_VERSION = require('./package.json').version

module.exports = defineConfig({
  publicPath: process.env.VUE_APP_PUBLIC_PATH,
  lintOnSave: false,
  outputDir: process.env.VUE_OUTPUT_DIR,
  // 生产环境的 source map（提升构建速度）
  productionSourceMap: false,

  // css配置
  css: {
    loaderOptions: {
      postcss: {
        postcssOptions: {
          plugins: [
            require("postcss-pxtorem")({
              //这里是配置项，详见官方文档
              rootValue: 37.5, //用于指定根元素的字体大小，以便计算rem单位
              propList: ["*"], // 需要转换的属性，*表示所有属性都需要转换
              exclude: ["node_modules"], //转换的时候排除
              unitPrecision: 3,
              selectorBlackList: [/^\.nop2r/, /^\.am/, "html", "body"], // 需要忽略的选择器，例如['.ignore']表示不转换.ignore类下的样式
              // replace: true,
              mediaQuery: false,
              minPixelValue: 1, // 小于2px的样式不进行转换
            }),
          ],
        },
      },
      sass: {
        // additionalData: '@import "~@/assets/scss/_globals.scss";'
      },
    },
  },
  // webpack-dev-server配置
  // https://commbi-d.beigene.cn/AIBackend/Api/
  // https://yuce001.zhi1soft.com/
  devServer: {
    open: {
      target: [`http://localhost:${PORT}`],
    },
    port: PORT,
    hot: true,
    compress: false,
    proxy: {
      "/AIBackend": {
        secure: true,
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          "^/AIBackend": "/AIBackend",
        },
      },
      "/AI": {
        secure: true,
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          "^/AI": "/AI",
        },
      },
      "/dev": {
        secure: true,
        target: process.env.VUE_APP_BASE_API,
        changeOrigin: true,
        pathRewrite: {
          "^/dev": "/dev",
        },
      },
    },
    // lint检查报错浏览器端提示
    client: {
      overlay: {
        warnings: true,
        errors: true,
      },
    },
  },
  chainWebpack: (config) => {
    // 配置全局默认title
    config.plugin("html").tap((args) => {
      args[0].title = TITLE;
      return args;
    });
    config.plugin("define").tap((definitions) => {
      Object.assign(definitions[0], {
        __VUE_OPTIONS_API__: "true",
        __VUE_PROD_DEVTOOLS__: "false",
        __VUE_PROD_HYDRATION_MISMATCH_DETAILS__: "false",
      });
      return definitions;
    });
    // 对于生产环境，开启内容基础的哈希
    if (process.env.NODE_ENV === 'production' || process.env.NODE_ENV === 'test') {
      // 修改打包的js、css文件名，增加内容哈希
      config.output.filename('js/[name].[contenthash:8].js').end();
      config.output.chunkFilename('js/[name].[contenthash:8].js').end();
      config.plugins.delete('extract-css');
      config.plugin('mini-css-extract-plugin').use(require('mini-css-extract-plugin'), [{
        filename: 'css/[name].[contenthash:8].css',
        chunkFilename: 'css/[name].[contenthash:8].css'
      }]);
    }
  },
  configureWebpack: {
    resolve: {
      alias: {
        views: "@/views", // @ 就是src(vue语法)
        assets: "@/assets", // @ 就是src(vue语法)
        components: "@/components",
      },
    },
    optimization: {
      splitChunks: {
        chunks: "async",
        cacheGroups: {
          render: {
            name: "render",
            test: /[\\/]node_modules[\\/]zrender[\\/]/,
            reuseExistingChunk: true,
            chunks: "all",
          },
        },
      },
    },
    plugins: [
      // Ignore all locale files of moment.js
      new webpack.IgnorePlugin({
        resourceRegExp: /^\.\/locale$/,
        contextRegExp: /moment$/,
      }),
    ],
  },
});
